<template>
  <div class="app-container">
    <eForm ref="form"/>
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入表名搜索" style="width: 200px;" class="filter-item"
                @keyup.enter.native="toQuery"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button
          v-permission="['admin','exportData:export']"
          :loading="exportDataLoading"
          :disabled="data.length === 0 || this.$refs.table.selection.length === 0"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-download"
          @click="generatorCode">数据生成
        </el-button>

      </div>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" ref="table" :data="data" size="small" style="width: 100%;">
      <el-table-column type="selection" width="55"/>
      <el-table-column label="表名" prop="tableName"/>
      <el-table-column label="Engine" prop="engine"/>
      <el-table-column label="备注" align="center" prop="tableComment"/>
      <el-table-column label="创建日期" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="this.checkPermission(['admin','',''])" label="操作" width="130px" align="center"
                       fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="['admin','']" size="mini" type="primary" icon="el-icon-download"
                     @click="generatorDataByTableName(scope.row.tableName)"/>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
    import checkPermission from '@/utils/permission'
    import initData from '@/mixins/initData'
    import {parseTime} from '@/utils/index'
    import {generatorCodeZip, getColumnInfoFromTableName} from '@/api/code-tools'
    import {downloadFileFunction} from '@/api/downloadFile'
    import eForm from './generatorData/form'

    export default {
        name: 'generatorData',
        components: {eForm},
        mixins: [initData],
        data() {
            return {
                query: {
                    value: '',
                    modelType: 'aidam'
                },
                exportDataLoading: false
            }
        },
        created() {
            this.$nextTick(() => {
                this.init()
            })
        },
        methods: {
            parseTime,
            checkPermission,
            beforeInit() {
                this.url = 'api/sys/generator/tableList';
                const sort = 'sort,asc';
                this.params = {page: this.page, size: this.size, sort: sort};
                const query = this.query;
                const value = query.value;
                if (value) {
                    this.params['tableName'] = value
                }
                return true
            },


            generatorCode() {
                this.doGeneratorCode()
            }
            ,
            generatorDataByTableName(tableName) {
                this.$refs.form.form = {'tableName': tableName, 'total': 100};
                this.$refs.form.dialog = true;
                this.$refs.form.getListData(tableName);
            }
            ,

            doGeneratorCode() {
                this.exportDataLoading = true
                const modelType = this.query.modelType;
                //this.checkModelTypeNotNull(modelType);
                const data = this.$refs.table.selection;
                const idsTableNames = [];
                for (let i = 0; i < data.length; i++) {
                    idsTableNames.push(data[i].tableName)
                }
                let outputFileName = idsTableNames + `代码文件.zip`;
                let generatorCodeParams = {
                    tableNames: idsTableNames, 'modelType': modelType
                };
                console.log(generatorCodeParams);
                this.commonGeneratorCodeZip(generatorCodeParams, outputFileName)
            }
            ,
            commonGeneratorCodeZip(generatorCodeParams, outputFileName) {
                console.log('aaa' + generatorCodeParams);
                generatorCodeZip(generatorCodeParams).then(res => {
                    this.exportDataLoading = false;
                    downloadFileFunction(res, outputFileName);
                    this.init();
                    this.$notify({
                        title: '下载成功',
                        type: 'success',
                        duration: 2500
                    })
                }).catch(err => {
                    this.exportDataLoading = false;
                    console.log(err.response.data.message)
                })
            }
        }
    }
</script>
